package cn.nawang.ebeim.ui.controller;

import cn.nawang.ebeim.client.dto.ClientDto;
import cn.nawang.ebeim.client.service.ClientService;
import cn.nawang.ebeim.persist.domain.DataSource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by GanJc on 2016/2/1.
 */
@RestController
@RequestMapping(value = "/api/download",produces="application/json;charset=UTF-8")
public class DownloadController {

    private static final Logger LOG = LoggerFactory.getLogger(DownloadController.class);

    @Resource
    private ClientDto dto ;

    @Resource
    private ClientService clientService ;

    @RequestMapping(value = "/list")
    public String list(){
        return JSON.toJSONString(dto.getDownloadProcesses());
    }

    @RequestMapping(value = "/dataSourceList")
    public String dataSourceList(){
        List<DataSource> dataSourceList = clientService.getDataSourceList();
        LOG.info("dataSourceList:" +dataSourceList);
        return JSON.toJSONString(dataSourceList);
    }


    @RequestMapping(value = "/start/{dsId}")
    public String start(@PathVariable String dsId){
        LOG.info("startDownload:" + dsId);
        clientService.startDownloadProcess(dsId);
        JSONObject json = new JSONObject();
        json.put("code", "200");
        return json.toString();
    }

    @RequestMapping(value = "/pause/{dsId}")
    public String pause(@PathVariable String dsId){
        LOG.info("pauseDownload:" + dsId);
        clientService.pauseDownloadProcess(dsId);
        JSONObject json = new JSONObject();
        json.put("code", "200");
        return json.toString();
    }

    @RequestMapping(value = "/delete/{dsId}")
    public String delete(@PathVariable String dsId){
        LOG.info("deleteDownload:" + dsId);
        clientService.removeDownloadProcess(dsId);
        JSONObject json = new JSONObject();
        json.put("code", "200");
        return json.toString();
    }

}
